package cn.zuo.view;

import cn.zuo.pojo.Message;
import cn.zuo.utils.*;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.net.Socket;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
 * @Author 白白旧维
 * @Date 2021/7/7 11:13
 */
public class RegisterJFrame {
    private JFrame jFrame;
    private JPanel mjp;

    public JPanel getMjp() {
        return mjp;
    }

    private JTextField name;
    private JTextField username;
    private JPasswordField password;
    private JPasswordField verifyPassword;
    private JButton btn_register;
    private JButton btn_cancel;
    private JLabel nameLabel;
    private JLabel usernameLabel;
    private JLabel passwordLabel;
    private JLabel verifyPasswordLabel;


    public RegisterJFrame(JFrame jFrame) {
        this.jFrame = jFrame;

        btn_cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //关闭此窗体
                jFrame.dispose();
            }
        });
        btn_register.addActionListener(new ActionListener() {
            @SneakyThrows
            @Override
            public void actionPerformed(ActionEvent ev) {
                //注册验证
                if(!verity()){
                    return;
                }

                //发送信息到服务器写入数据库
                String sname = name.getText().trim();
                String susername = username.getText().trim();


                Socket s=null;

                try {
                    s = new Socket("127.0.0.1",9999);


                    ObjectInputStream ois1 = new ObjectInputStream(s.getInputStream());


                    //先接收服务端的公钥
                    Message key_msg = (Message) ois1.readObject();
                    BigInteger server_e = new BigInteger(key_msg.getMsg().split("_")[0]);
                    BigInteger server_n = new BigInteger(key_msg.getMsg().split("_")[1]);
                    PublicKeyPool.setServer_key_e(server_e);
                    PublicKeyPool.setServer_key_n(server_n);

                    //发送自己的公钥给服务端
                    ObjectOutputStream oos1 = new ObjectOutputStream(s.getOutputStream());
                    Map<String, BigInteger> key = PPKeyFactory.getOneKey(System.currentTimeMillis() + new Random().nextLong());
                    BigInteger e = key.get("e");
                    BigInteger n = key.get("n");
                    oos1.writeObject(new Message(susername,null,e+"_"+n));


                    //加密和签名
                    String spassword = String.valueOf(password.getPassword()).trim();
//                    String spassword_hm = MD5.md5(spassword);//哈希
                    String spassword_hm = SHA256.sha256(spassword);//哈希

                    List<BigInteger> spassword_hm_signed = RSA.signed_mul(key.get("d"), key.get("n"), spassword_hm);
                    String spassword_hm_signed_json = JSON.toJSONString(spassword_hm_signed);//签名

                    String spassword_and_signed = spassword+"_"+spassword_hm_signed_json;//密码信息和签名合并

                    List<BigInteger> en_spassword_and_signed = RSA.encryption_mul(PublicKeyPool.getServer_key_e(), PublicKeyPool.getServer_key_n(), spassword_and_signed);
                    String en_spassword_and_signed_json = JSON.toJSONString(en_spassword_and_signed);//加密

                    Message msg = new Message(sname+"__"+susername,"register",en_spassword_and_signed_json);


                    ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
                    oos.writeObject(msg);//注册信息

                    ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
                    Message message = (Message) ois.readObject();//服务端响应信息

                    if(!"0".equals(message.getMsg())){//注册成功
                        //关闭窗口
                        jFrame.dispose();
                    }else {
                        JOptionPane.showMessageDialog(null,"注册失败");
                    }

                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(null,"服务器未开启或发生未知错误");
//                    throw new Exception("服务器未开启或发生未知错误");
                    ex.printStackTrace();
                }finally {
                    s.close();
                }




            }
        });
    }


    public boolean verity(){
        if("".equals(name.getText().trim())){
            JOptionPane.showMessageDialog(null,"请输入用户名");
            return false;
        }
        if("".equals(username.getText().trim())){
            JOptionPane.showMessageDialog(null,"请输入账号");
            return false;
        }
        if("".equals(String.valueOf(password.getPassword()).trim())||password.getPassword().length==0){
            JOptionPane.showMessageDialog(null,"请输入密码");
            return false;
        }
        if("".equals(String.valueOf(verifyPassword.getPassword()).trim())||verifyPassword.getPassword().length==0){
            JOptionPane.showMessageDialog(null,"请输入确认密码");
            return false;
        }
        if(!String.valueOf(password.getPassword()).trim().equals(String.valueOf(verifyPassword.getPassword()).trim())){
            JOptionPane.showMessageDialog(null,"密码和确认密码不匹配");
            return false;
        }
        return true;

    }

}
